import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { VersioningType } from '@nestjs/common';
import { TransformInterceptor } from './common/interceptors/transform.interceptor';
import { HttpExceptionFilter } from './common/exceptions/http.exception.filter';
import { AllExceptionsFilter } from './common/exceptions/base.exception.filter';

import * as session from 'express-session';
import * as express from 'express';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';

async function bootstrap() {
    const app = await NestFactory.create(AppModule,
        {
            cors: true,//支持跨域
        });

    // 全局拦截器
    app.useGlobalInterceptors(new TransformInterceptor())
    // 异常过滤器
    app.useGlobalFilters(new AllExceptionsFilter(), new HttpExceptionFilter());
    // 设置全局路由前缀为 /api
    app.setGlobalPrefix('api');
    //版本号
    app.enableVersioning({ type: VersioningType.URI });
    // 使用session
    app.use(
        session({
            secret: 'my-secret',
            resave: false,
            saveUninitialized: false,

            rolling: true, //在每次请求时强行设置 cookie，这将重置 cookie 过期时间(默认:false)
        }),
    );

    // swigger
    // 设置swagger文档
    const config = new DocumentBuilder()
        .setTitle('IM系统')
        .setDescription('IM系统接口文档')
        .setVersion('1.0')
        .addBearerAuth()
        .build();
    const document = SwaggerModule.createDocument(app, config);
    SwaggerModule.setup('docs', app, document);//访问地址ttp://localhost:3000/docs


    // 资源访问地址便于用户直接使用 http://localhost:3000/public/uploads/2024-03-27/1711520213832.jpg
    app.use('/public', express.static(`${__dirname}/../public`));


    await app.listen(3000);
}
bootstrap();